package org.freez4.spring.ioc.overview.dependency.inject;

import org.freez4.spring.ioc.overview.domain.User;
import org.freez4.spring.ioc.overview.repository.UserRepository;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 依赖注入Demo。
 *
 * @author 阿坤同学
 * @date 2023/12/3 21:38
 */
public class DependencyInjectionDemo {

    private static final Logger logger = Logger.getLogger(DependencyInjectionDemo.class.getName());

    public static void main(String[] args) {
        ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext("META-INF/dependency-injection-context.xml");
        beanFactory.refresh();

        // 依赖注入
        UserRepository userRepository = beanFactory.getBean("userRepository", UserRepository.class);
        logger.log(Level.INFO, () -> "userRepository实时注入信息: " + userRepository);

        User user = userRepository.getObjectFactory().getObject();
        logger.log(Level.INFO, () -> "通过ObjectFactory延迟注入信息: " + user);

        logger.log(Level.INFO, () -> "IoC 容器内部依赖" + userRepository.getBeanFactory());

        logger.log(Level.INFO, () -> "通过ObjectFactory延迟注入ApplicationContext是否与当前上下文beanFactory相等？" +
                (beanFactory == userRepository.getApplicationContextObjectFactory().getObject()));
    }
}
